package com.example.netty;

import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

public class PromiseDemo {

    private static final Logger log = LoggerFactory.getLogger(PromiseDemo.class);

    public static void main(String[] args) throws InterruptedException {
        NioEventLoopGroup group = new NioEventLoopGroup(2);
        // 指定一个线程
        DefaultPromise<Integer> promise = new DefaultPromise<>(group.next());
        new Thread(() -> {
            promise.setSuccess(100);
            //
            log.info("异步的线程的结束了");
        }).start();

        TimeUnit.SECONDS.sleep(2);
        log.info("注册监听器");
        promise.addListener(future -> {
            log.info("result is:{}", future.get());
        });
        log.info("-_- 当前main线程已经结束了");
    }
}
